package com.cosytek.farmmanager;

import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * Created by Happy on 2015/3/24.
 */
public class ServerRequest {
    private ServerRequest(IServerRequestListener listener, String path) {
        mListener = listener;
        mPath = path;
    }

    private final IServerRequestListener mListener;
    private HttpUriRequest mRequest;
    private HttpTask mTask;
    private final String mPath;
    private String mTag;

    public String path() {
        return mPath;
    }

    public String tag() {
        return mTag;
    }

    public void setTag(String tag) {
        mTag = tag;
    }

    public interface IServerRequestListener {
        public void onRequestCompleted(ServerRequest r, boolean success, ServerError error, String respBody);
    }

    public static ServerRequest newGetRequest(IServerRequestListener listener, String token, String path) {
        ServerRequest r = new ServerRequest(listener, path);
        String serverAddr = FarmManagerApp.getAppContext().getString(R.string.server_address);
        r.mRequest = new HttpGet(serverAddr + path);
        r.mRequest.addHeader("tk", token);

        return r;
    }

    public static ServerRequest newPostRequest(IServerRequestListener listener, String token, String path) {
        ServerRequest r = new ServerRequest(listener, path);
        String serverAddr = FarmManagerApp.getAppContext().getString(R.string.server_address);
        r.mRequest = new HttpPost(serverAddr+path);
        r.mRequest.addHeader("tk", token);
        return r;
    }

    public static  ServerRequest newAuthorizeRequest(IServerRequestListener listener, String path,
                                                     String account, String password) {
        ServerRequest r = new ServerRequest(listener, path);
        String serverAddr = FarmManagerApp.getAppContext().getString(R.string.server_address);
        r.mRequest = new HttpGet(serverAddr+path);
        r.mRequest.addHeader("account", account);
        r.mRequest.addHeader("password", password);

        return r;
    }

    public void execute() {
        mTask = new HttpTask();
        mTask.execute((Void) null);
    }

    public class HttpTask extends AsyncTask<Void, Void, Boolean> {

        private String mRespBody;
        private ServerError mRespError;

        @Override
        protected Boolean doInBackground(Void... params) {
            AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
            try {
                HttpResponse resp = client.execute(mRequest);
                if (ServerError.hasError(resp)) {
                    mRespError = ServerError.parseServerError(resp);
                    return false;
                }

                HttpEntity entity = resp.getEntity();
                if (entity != null) {
                    mRespBody = EntityUtils.toString(entity, "UTF-8");
                }

                return true;
            } catch (IOException e) {
                mRespError = ServerError.genericNetworkError();
                e.printStackTrace();
            } finally {
                client.close();
            }
            return false;
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            onHttpTaskCompleted(success, mRespError, mRespBody);
        }

        @Override
        protected void onCancelled() {
            onHttpTaskCompleted(false, mRespError, mRespBody);
        }
    }

    private void onHttpTaskCompleted(Boolean success, ServerError respError, String respBody) {
        mListener.onRequestCompleted(this, success, respError, respBody);
    }
}
